package org.xteam.sled.model;

import org.xteam.sled.semantic.GlobalState;
import org.xteam.sled.semantic.exp.Exp;
import org.xteam.sled.semantic.exp.ExpAdd;
import org.xteam.sled.semantic.exp.ExpConst;
import org.xteam.sled.semantic.exp.ExpPC;

public class PatternUtils {

	public static Exp addBitsToPC(int bits) {
		if (bits == 0)
			return new ExpPC();
		return new ExpAdd(new ExpPC(), new ExpConst(bitsToPCunits(bits)));
	}

	public static int bitsToPCunits(int bits) {
		int unit = GlobalState.pcUnitBits;
		int pcs = bits / unit;
		if (pcs * unit != bits) {
			throw new RuntimeException("Token of size " + bits
					+ " is not integral number of " + unit
					+ "-bit units used to increment PC");
		}
		return pcs;
	}
}
